package org.fiel.admin.dao;

import java.io.PrintStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.fiel.admin.struts.forms.TipoForm;
import org.fiel.domain.Tipo;

public class TipoDAO
{

    public TipoDAO()
        throws Exception
    {
        conn = null;
        try
        {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/FielDB");
            conn = ds.getConnection();
        }
        catch(Exception e)
        {
            System.err.println("TipoDAO() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO() Error:\n" + e.getMessage());
        }
    }

    public void createTipo(TipoForm tipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("INSERT INTO tipos (Descripcion, Codigo) VALUES(?,?)");
            pstmt.setString(1, tipo.getTipo());
            pstmt.setString(2, tipo.getCodigo());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("TipoDAO.createTipo() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO.createTipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public List retrieveTipos()
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List tipos = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM tipos ORDER BY Descripcion ASC");
            Tipo tipo;
            for(rset = pstmt.executeQuery(); rset.next(); tipos.add(tipo))
            {
                tipo = new Tipo();
                tipo.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                tipo.setDescripcion(rset.getString("Descripcion"));
                tipo.setCodigo(rset.getString("Codigo"));
            }

        }
        catch(Exception e)
        {
            System.err.println("TipoDAO.retrieveTipos() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO.retrieveTipos() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return tipos;
    }

    public Tipo retrieveTipo(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        Tipo tipo = null;
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM tipos WHERE Identipo = ?");
            pstmt.setString(1, id);
            rset = pstmt.executeQuery();
            if(rset.next())
            {
                tipo = new Tipo();
                tipo.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                tipo.setDescripcion(rset.getString("Descripcion"));
                tipo.setCodigo(rset.getString("Codigo"));
            }
        }
        catch(Exception e)
        {
            System.err.println("TipoDAO.retrieveTipo() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO.retrieveTipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return tipo;
    }

    public void updateTipo(TipoForm tipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("UPDATE tipos SET Descripcion = ?, Codigo = ? WHERE Identipo = ?");
            pstmt.setString(1, tipo.getTipo());
            pstmt.setString(2, tipo.getCodigo());
            pstmt.setString(3, tipo.getId());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("TipoDAO.updateTipo() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO.updateTipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public void deleteTipo(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("DELETE FROM tipos WHERE Identipo = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("TipoDAO.deleteTipo() Error:\n" + e.getMessage());
            throw new Exception("TipoDAO.deleteTipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    private Connection conn;
}
